package gbench.appdemo.mall.erp.components;

import static gbench.common.tree.LittleTree.Term.FT;

import gbench.common.matlib.data.DataReader.DFrame;

public interface IWareHouse extends IMyERPEntity {
    
    /**
     * 
     * @return
     */
    public Integer getId();

    /**
     * 产品名称
     * 
     * @return
     */
    public String getName();
    
    /**
     * 试算平衡
     * @return
     */
    public default DFrame trialBalance() {
        final var sql = FT("SELECT item_id id,"
                + " sum(if(drcr=0,1,-1) * quantity) quantity, "
                + " sum(if(drcr=0,1,0) * quantity) dr,"
                + " sum(if(drcr=0,0,1) * quantity) cr "
                + " FROM tbl$ "
                + " where entity_id=$0 group by item_id",this.getId());
        final var _sql = FT("select t.id,p.name,t.dr,t.cr,t.quantity"
                + " from ($0) t inner join t_product p on t.id = p.id"
                + " order by quantity desc",sql);
        final var dataApp = this.dataApp();
        final var dd = dataApp.rel2df_sql_f("t_house", "t_product")
                .apply(_sql);
        return dd;
    }
    
    /**
     * 库房日记账
     * @return
     */
    public default DFrame houseJounal(IEnterprise enterprise) {
        final var order_tbl = FT("select if(partA=$0,1,-1) drcr,a.* from t_contract a where partA=$0 or partB=$0",enterprise.getId()); // 订单表
        final var t = FT("select tbl.id, o.partA, o.partB, o.drcr o_drcr,tbl.drcr i_drcr, o.id house_id, tbl.item_id, tbl.quantity,tbl.optional order_id, tbl.create_time "
                + "from tbl$ inner join ($0) o on o.id = tbl.optional",order_tbl); // o.id = tbl.optional 表示:出库记录 中的 optional 对应于 订单表t_contract的id
        // println(t);
        final var dataApp = this.dataApp();
        return dataApp.rel2df_sql_f("t_house", "t_product").apply(t);
    }
    
    /**
     * 获取地址
     * @return
     */
    public String getAddress();

}
